<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <customDeptTree @clickDeptNode="handleNodeClick" :show-search-input="true"/>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="120px">
          <el-form-item label="手机号" prop="account">
            <el-input v-model="queryParams.account" placeholder="请输入手机号" clearable size="small"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="网关MAC" prop="gatewayMac">
            <el-input v-model="queryParams.gatewayMac" placeholder="请输入网关MAC" clearable size="small"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="设备名称或MAC" prop="deviceNameOrMac">
            <el-input v-model="queryParams.deviceNameOrMac" placeholder="请输入设备名称或MAC" clearable size="small"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button class="filter-item" type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
            >搜索
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
                       v-hasPermi="['system:detail:joint:export']"
            >导出
            </el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10">
        </el-row>
        <el-col>
          <el-table v-loading="loading" :data="relationList" border row-key="${treeCode}" default-expand-all
          >
            <el-table-column label="手机号" align="center" prop="account" width="120"/>
            <el-table-column label="家庭名称" align="center" prop="homeName" :show-overflow-tooltip="true"/>
            <el-table-column label="网关MAC" align="center" prop="gatewayMac" :show-overflow-tooltip="true"/>
            <el-table-column label="主动设备" align="center" prop="startDeviceName" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <template v-if="scope.row.startDeviceName">
                  <el-popover trigger="hover" placement="bottom-start">
                    <el-descriptions :column=1 style="width: 250px" :labelStyle="{'font-weight': 'bold'}">
                      <el-descriptions-item label="设备名称">{{ scope.row.startDeviceName }}</el-descriptions-item>
                      <el-descriptions-item label="设备名MAC">{{ scope.row.startDeviceMac }}</el-descriptions-item>
                      <el-descriptions-item label="设备类型">{{ scope.row.startDeviceType }}</el-descriptions-item>
                      <el-descriptions-item label="类型名称">{{ scope.row.startDeviceCategoryName }}</el-descriptions-item>
                      <el-descriptions-item label="开关路数">{{ scope.row.startDeviceNum }}</el-descriptions-item>
                    </el-descriptions>

                    <span slot="reference">
                    {{ scope.row.startDeviceName }}
                    </span>
                  </el-popover>
                </template>
                <template v-else>
                  <el-tag type="info">未知</el-tag>
                </template>
              </template>
            </el-table-column>
            <el-table-column label="被动设备" align="center" prop="endDeviceName" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <template v-if="scope.row.endDeviceName">
                  <el-popover trigger="hover" placement="bottom-start">
                    <el-descriptions :column=1 style="width: 250px" :labelStyle="{'font-weight': 'bold'}">
                      <el-descriptions-item label="设备名称">{{ scope.row.endDeviceName }}</el-descriptions-item>
                      <el-descriptions-item label="设备名MAC">{{ scope.row.endDeviceMac }}</el-descriptions-item>
                      <el-descriptions-item label="设备类型">{{ scope.row.endDeviceType }}</el-descriptions-item>
                      <el-descriptions-item label="类型名称">{{ scope.row.endDeviceCategoryName }}</el-descriptions-item>
                      <el-descriptions-item label="开关路数">{{ scope.row.endDeviceNum }}</el-descriptions-item>
                    </el-descriptions>

                    <span slot="reference">
                  {{ scope.row.endDeviceName }}
                </span>
                  </el-popover>
                </template>
                <template v-else>
                  <el-tag type="info">未知</el-tag>
                </template>
              </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
            <el-table-column label="操作" align="center" width="100" v-if="false">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetailsView(scope.row)"
                           v-hasPermi="['system:detail:joint:detail']"
                >详情
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                      :limit.sync="queryParams.pageSize" @pagination="getList"
          />
        </el-col>
      </el-col>
    </el-row>

    <!-- 详情多联多控对话框 -->
    <el-dialog :title="title" :visible.sync="details" width="800px" append-to-body>
      <template v-if="form1">
        <el-descriptions title="主动设备信息" :column=3 contentClassName="my-content" labelClassName="my-label">
          <el-descriptions-item label="设备名称">{{ form1.deviceName }}</el-descriptions-item>
          <el-descriptions-item label="MAC地址">{{ form1.deviceMac }}</el-descriptions-item>
          <el-descriptions-item label="设备类型">
            <template>
              {{ form1.deviceType }}
            </template>
            <template>
              <el-tag type="success"> {{ form1.defaultDeviceName }}</el-tag>
            </template>
          </el-descriptions-item>
          <el-descriptions-item label="设备状态">
            <template>
              <div v-html="fetchStatusHtml1">
              </div>
            </template>
          </el-descriptions-item>
          <el-descriptions-item label="操作时间">{{ form1.switchTime }}</el-descriptions-item>
          <el-descriptions-item label="在网状态">
            <template v-if="form1.lwbz==='0'">
              <template v-if="form1.onlineType!=='3'">
                <template v-if="form1.diffTime > (form1.onlineTime * 60)">
                  <div class="myTagOffline">离线</div>
                </template>
                <template v-else>
                  <div class="myTagOnline">在线</div>
                </template>
              </template>
            </template>
            <template v-else>
              <div class="myTagOffNet">离网</div>
            </template>
          </el-descriptions-item>
          <el-descriptions-item label="推送状态">
            <template v-if="form1.pushType==='1'">
              <div class="myTagOpen">都推送</div>
            </template>
            <template v-else-if="form1.pushType==='2'">
              <div class="myTagOpen">布防</div>
            </template>
            <template v-else-if="form1.pushType==='3'">
              <div class="myTagOpen">撤防</div>
            </template>
            <template v-else-if="form1.pushType==='3'">
              <div class="myTagOpen">不推送</div>
            </template>
            <template v-else>
              <div class="myTagClose">未知</div>
            </template>
          </el-descriptions-item>
          <el-descriptions-item label="联动类型">
            <template v-if="form1.spreadingType==='02'">
              <div class="myTagOpen">可联动</div>
            </template>
            <template v-else>
              <div class="myTagClose">不可联动</div>
            </template>
          </el-descriptions-item>
          <el-descriptions-item label="创建时间">{{ form1.createTime }}</el-descriptions-item>
          <el-descriptions-item label="修改时间">{{ form1.modifyTime }}</el-descriptions-item>
        </el-descriptions>
        <el-divider/>
      </template>
      <template v-if="form2">
        <el-descriptions title="被动设备信息" :column=3 contentClassName="my-content" labelClassName="my-label">
          <el-descriptions-item label="设备名称">{{ form2.deviceName }}</el-descriptions-item>
          <el-descriptions-item label="MAC地址">{{ form2.deviceMac }}</el-descriptions-item>
          <el-descriptions-item label="设备类型">
            <template>
              {{ form2.deviceType }}
            </template>
            <template>
              <el-tag type="success"> {{ form2.defaultDeviceName }}</el-tag>
            </template>
          </el-descriptions-item>
          <el-descriptions-item label="设备状态">
            <template>
              <div v-html="fetchStatusHtml2">
              </div>
            </template>
          </el-descriptions-item>
          <el-descriptions-item label="操作时间">{{ form2.switchTime }}</el-descriptions-item>
          <el-descriptions-item label="在网状态">
            <template v-if="form2.lwbz==='0'">
              <template v-if="form2.onlineType!=='3'">
                <template v-if="form2.diffTime > (form2.onlineTime * 60)">
                  <div class="myTagOffline">离线</div>
                </template>
                <template v-else>
                  <div class="myTagOnline">在线</div>
                </template>
              </template>
            </template>
            <template v-else>
              <div class="myTagOffNet">离网</div>
            </template>
          </el-descriptions-item>
          <el-descriptions-item label="推送状态">
            <template v-if="form2.pushType==='1'">
              <div class="myTagOpen">都推送</div>
            </template>
            <template v-else-if="form2.pushType==='2'">
              <div class="myTagOpen">布防</div>
            </template>
            <template v-else-if="form2.pushType==='3'">
              <div class="myTagOpen">撤防</div>
            </template>
            <template v-else-if="form2.pushType==='3'">
              <div class="myTagOpen">不推送</div>
            </template>
            <template v-else>
              <div class="myTagClose">未知</div>
            </template>
          </el-descriptions-item>
          <el-descriptions-item label="联动类型">
            <template v-if="form2.spreadingType==='02'">
              <div class="myTagOpen">可联动</div>
            </template>
            <template v-else>
              <div class="myTagClose">不可联动</div>
            </template>
          </el-descriptions-item>
          <el-descriptions-item label="创建时间">{{ form2.createTime }}</el-descriptions-item>
          <el-descriptions-item label="修改时间">{{ form2.modifyTime }}</el-descriptions-item>
        </el-descriptions>
      </template>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { exportRelation, listRelation } from '@/api/system/relation'
import { fetchStatus, getDeviceByMacAndWay } from '@/api/system/device'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'Relation',
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 多联多控表格数据
      relationList: [],
      // 多联多控树选项
      relationOptions: [],
      // 部门名称
      deptName: undefined,
      // 部门树选项
      deptOptions: undefined,
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      details: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        account: undefined,
        gatewayMac: undefined,
        deviceNameOrMac: undefined,
        deptId: undefined
      },
      fetchStatusHtml1: '',
      fetchStatusHtml2: '',
      // 表单参数
      form1: {},
      form2: {}
    }

  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询多联多控列表 */
    getList() {
      this.loading = true
      listRelation(this.queryParams).then(response => {
        this.relationList = response.rows
        this.total = response.total
        this.loading = false
      })

    },
    // 取消按钮
    cancel() {
      this.open = false
      this.details = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        startDeviceId: undefined,
        endDeviceNum: undefined,
        createTime: undefined,
        modifyTime: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 详情按钮操作 */
    handleDetailsView(row) {
      this.reset()
      this.form1 = null
      this.form2 = null
      const startDeviceMac = row.startDeviceMac
      const startDeviceWayNum = row.startDeviceNum
      const endDeviceMac = row.endDeviceMac
      const endDeviceWayNum = row.endDeviceNum
      getDeviceByMacAndWay({ zigbeeMac: startDeviceMac, wayNum: startDeviceWayNum }).then(response => {
        this.form1 = response.data
        this.details = true
        this.title = '联控详情'
        // this.actions = form.actions
        // this.deviceType = form.deviceType
        // this.gatewayMac = row.gatewayMac
        if (this.form1) {
          this.fetchStatusHtml1 = fetchStatus(this.form1.actions, this.form1.deviceType)
        }
      })
      getDeviceByMacAndWay({ zigbeeMac: endDeviceMac, wayNum: endDeviceWayNum }).then(response => {
        this.form2 = response.data
        this.details = true
        this.title = '联控详情'
        // this.actions = form.actions
        // this.deviceType = form.deviceType
        // this.gatewayMac = row.gatewayMac
        if (this.form2) {
          this.fetchStatusHtml2 = fetchStatus(this.form2.actions, this.form2.deviceType)
        }
      })
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出数据项?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportRelation(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id
      this.getList()
    }
  }
}
</script>
